package com.fdb.efp.console.api.rest;

import java.util.List;
import java.util.Map;

import com.irdstudio.basic.framework.core.constant.PubConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.irdstudio.efp.console.service.facade.PrdAdaptedSoltService;
import com.irdstudio.efp.console.service.vo.PrdAdaptedSoltVO;
import com.irdstudio.efp.e4a.service.facade.SRoleDataRuleService;
import com.irdstudio.basic.framework.core.constant.DataRuleType;
import com.irdstudio.basic.framework.core.constant.ResponseData;
import com.irdstudio.basic.framework.core.util.StringUtil;
import com.irdstudio.basic.framework.core.util.TimeUtil;
import com.irdstudio.basic.framework.web.controller.AbstractController;

/**
 * Controller：产品插槽
 * 
 * @author huangwb
 * @since 2018年12月26日 下午5:38:21
 * @version 1.0
 */
@RestController
@RequestMapping("/api")
public class PrdAdaptedSoltController extends AbstractController {
	@Autowired
	@Qualifier("sRoleDataRuleService")
	private SRoleDataRuleService sRoleDataRuleService;

	@Autowired
	@Qualifier("prdAdaptedSoltService")
	private PrdAdaptedSoltService prdAdaptedSoltService;

	/**
	 * 列表数据查询<br>
	 * 
	 * @param page
	 * @param size
	 * @return
	 */
	@PostMapping(value = "/prd/adapted/solts")
	public @ResponseBody ResponseData<List<PrdAdaptedSoltVO>> queryPrdAdaptedSoltAll(@RequestBody PrdAdaptedSoltVO vo) {
		// 根据用户角色获取用户权限方案
		List<PrdAdaptedSoltVO> outputVo = null;
		Map<String,String> dataRuleTypeMap = sRoleDataRuleService.getDataRuleType(httpRequest.getRequestURI(), getUserRoles());
		vo.setPrdCodebyBaseInfo(dataRuleTypeMap == null ? null:dataRuleTypeMap.get(PubConstant.PRD_CODE));
		DataRuleType dataRuleType = DataRuleType.getDataRuleType(dataRuleTypeMap == null ? null:dataRuleTypeMap.get(PubConstant.PERMIS_TYPE));
		setUserInfoToVO(vo);
		// 根据权限方案调用不同的方案进行数据查询
		switch (dataRuleType) {
		case Owner:
			outputVo = prdAdaptedSoltService.queryAllOwner(vo);
			break;
		case CurrOrg:
			outputVo = prdAdaptedSoltService.queryAllCurrOrg(vo);
			break;
		case CurrDownOrg:
			outputVo = prdAdaptedSoltService.queryAllCurrDownOrg(vo);
			break;
		default:
			outputVo = prdAdaptedSoltService.queryAllOwner(vo);
			break;
		}
		return getResponseData(outputVo);

	}

	/**
	 * 根据主键查询详情
	 * 
	 * @return
	 */
	@GetMapping(value = "/prd/adapted/solt/{prdId}/{soltCode}")
	public @ResponseBody ResponseData<PrdAdaptedSoltVO> queryByPk(@PathVariable("prdId") String prdId,
			@PathVariable("soltCode") String soltCode) {
		PrdAdaptedSoltVO inVo = new PrdAdaptedSoltVO();
		inVo.setPrdId(prdId);
		inVo.setSoltCode(soltCode);
		PrdAdaptedSoltVO outputVo = prdAdaptedSoltService.queryByPk(inVo);
		return getResponseData(outputVo);

	}

	/**
	 * 根据主键删除信息
	 * 
	 * @param prdAdaptedSolt
	 * @return
	 */
	@DeleteMapping(value = "/prd/adapted/solt")
	public @ResponseBody ResponseData<Integer> deleteByPk(@RequestBody PrdAdaptedSoltVO inPrdAdaptedSoltVo) {
		/** 处理前端传入的主键参数 */
		if(StringUtil.isNotEmpty(inPrdAdaptedSoltVo.getPrdId())) {
			String[] temp = inPrdAdaptedSoltVo.getPrdId().split(",");
			inPrdAdaptedSoltVo.setPrdId(temp[0]);
			inPrdAdaptedSoltVo.setSoltCode(temp[1]);
		}
		int outputVo = prdAdaptedSoltService.deleteByPk(inPrdAdaptedSoltVo);
		return getResponseData(outputVo);

	}

	/**
	 * 根据主键更新信息
	 * 
	 * @param inPrdAdaptedSoltVo
	 * @return
	 */
	@PutMapping(value = "/prd/adapted/solt")
	public @ResponseBody ResponseData<Integer> updateByPk(@RequestBody PrdAdaptedSoltVO inPrdAdaptedSoltVo) {
		setUserInfoToVO(inPrdAdaptedSoltVo);
		inPrdAdaptedSoltVo.setLastModifyUser(inPrdAdaptedSoltVo.getLoginUserId());
		inPrdAdaptedSoltVo.setLastModifyTime(TimeUtil.getCurrentDateTime());
		int outputVo = prdAdaptedSoltService.updateByPk(inPrdAdaptedSoltVo);
		return getResponseData(outputVo);

	}

	/**
	 * 新增数据
	 * 
	 * @param inPrdAdaptedSoltVo
	 * @return
	 */
	@PostMapping(value = "/prd/adapted/solt")
	public @ResponseBody ResponseData<Integer> insertPrdAdaptedSolt(@RequestBody PrdAdaptedSoltVO inPrdAdaptedSoltVo) {
		setUserInfoToVO(inPrdAdaptedSoltVo);
		inPrdAdaptedSoltVo.setLastModifyUser(inPrdAdaptedSoltVo.getLoginUserId());
		inPrdAdaptedSoltVo.setLastModifyTime(TimeUtil.getCurrentDateTime());
		int outputVo = prdAdaptedSoltService.insertPrdAdaptedSolt(inPrdAdaptedSoltVo);
		return getResponseData(outputVo);

	}
}
